package com.hzit.servlet;

import com.alibaba.fastjson.JSON;
import com.hzit.dto.ClassesDTO;
import com.hzit.entity.R;
import com.hzit.service.IClassesService;
import com.hzit.service.impl.ClassesServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/classes")
public class ClassesServlet extends HttpServlet {
	private IClassesService classesService;
	@Override
	public void init() throws ServletException {
		classesService = new ClassesServiceImpl();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 查询所有班级
		resp.setContentType("application/json;charset=utf8");
		List<ClassesDTO> dtoList = classesService.findAll();
		//2. 放到r对象中
		R<List<ClassesDTO>> r = R.ok(dtoList);
		//3. 转换为字符串并响应到前端
		resp.getWriter().println(JSON.toJSONString(r));
		resp.getWriter().flush();
	}
}
